package com.joysuch.wwyt.bp.service.impl;

import com.joysuch.wwyt.bp.bean.inspect.BPInspectItemBean;
import com.joysuch.wwyt.bp.bean.inspect.BPInspectItemQueryBean;
import com.joysuch.wwyt.bp.entity.inspect.BPInspectItem;
import com.joysuch.wwyt.bp.entity.inspect.BPInspectItemType;
import com.joysuch.wwyt.bp.entity.inspect.BpInspectType;
import com.joysuch.wwyt.bp.repository.BPInspectItemDao;
import com.joysuch.wwyt.bp.repository.BPInspectItemTypeDao;
import com.joysuch.wwyt.bp.service.BPInspectItemService;
import com.joysuch.wwyt.core.support.Context;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 巡检： 检查项类型Service
 */

@Slf4j
@Service
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class BPInspectItemServiceImpl implements BPInspectItemService {

    @Autowired
    private BPInspectItemDao inspectItemDao;

    @Autowired
    private BPInspectItemTypeDao inspectItemTypeDao;


    @Override
    public Page<BPInspectItemBean> findList(BPInspectItemQueryBean queryBean, Pageable pageable) {
        Page<BPInspectItem> pageList = null;
        if(queryBean.getTypeId() == null || queryBean.getTypeId() ==0){
            pageList= inspectItemDao.findAll(pageable);
        }else {
            Specification<BPInspectItem> specification = new Specification<BPInspectItem>() {
                @Override
                public Predicate toPredicate(Root<BPInspectItem> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                    List<Predicate> predicates = new ArrayList<>();
                    if (queryBean.getTypeId() != null && queryBean.getTypeId() != 0) {
                        predicates.add(criteriaBuilder.equal(root.get("inspectType"), queryBean.getTypeId()));
                    }
                    predicates.add(criteriaBuilder.equal(root.get("deleteFlag"), "0"));
                    predicates.add(criteriaBuilder.equal(root.get("orgCode"), Context.getCurrentOrgCode()));
                    predicates.add(criteriaBuilder.equal(root.get("tenentId"), Context.getCurrentTenantId()));
                    return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
                }
            };
            Sort.by(Sort.Order.desc("createTime"));
            pageList= inspectItemDao.findAll(specification, pageable);
        }
        List<BPInspectItemBean> beanList = new ArrayList<>();
        for(BPInspectItem inspectItem: pageList){
            BPInspectItemBean bean  = new BPInspectItemBean();
            bean.setId(inspectItem.getId());
            bean.setInspectItem(inspectItem.getInspectItem());
            bean.setInspectType(inspectItem.getInspectType());
            bean.setSeqNo(inspectItem.getSeqNo());
            bean.setInspectStandard(inspectItem.getInspectStandard());
            if(inspectItem.getInspectType() != null){
                Optional<BPInspectItemType> inspectType = inspectItemTypeDao.findById(inspectItem.getInspectType());
                if(inspectType.isPresent()){
                    bean.setInspectTypeName(inspectType.get().getItemType());
                }
            }
            beanList.add(bean);
        }

        return  new PageImpl<BPInspectItemBean>(beanList, pageable, pageList.getTotalElements());
    }

    @Override
    public BPInspectItem save(BPInspectItemBean bean){
        BPInspectItem entity = null;
        if(bean.getId() == null || bean.getId()== 0){
            entity = new BPInspectItem();
            entity.setInspectType(bean.getInspectType());
            Integer seed = inspectItemDao.getMaxSeq();
            entity.setSeqNo(seed == null ? 0 : ++seed);
        }else{
            entity = inspectItemDao.findById(bean.getId()).get();
        }
        entity.setInspectItem(bean.getInspectItem());
        entity.setInspectStandard(bean.getInspectStandard());
        return inspectItemDao.save(entity);
    }

    @Override
    public void saveList( List<BPInspectItemBean> beanList) {
         List<BPInspectItem> list = new ArrayList<>();

        beanList.stream().forEach(item->{
            BPInspectItem entity = new BPInspectItem();
            entity.setSeqNo(item.getSeqNo());
            entity.setInspectItem(item.getInspectItem());
            entity.setInspectType(item.getInspectType());
            entity.setInspectStandard(item.getInspectStandard());
            list.add(entity);
        });
        inspectItemDao.saveAll(list);
    }

    @Override
    public void deleteById(Long id){

        inspectItemDao.deleteById(id);
    }

    @Override
    public BPInspectItem getById(Long id) {
        Optional<BPInspectItem> optional =   inspectItemDao.findById(id);
        return optional.isPresent() ? optional.get() : null;
    }


}
